{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Visualisation of many initial condition for 3d ODE"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/marcin/anaconda3/lib/python3.7/site-packages/traittypes/traittypes.py:101: UserWarning: Given trait value dtype \"float64\" does not match required type \"float32\". A coerced copy has been created.\n",
      "  np.dtype(self.dtype).name))\n",
      "/Users/marcin/anaconda3/lib/python3.7/site-packages/traittypes/traittypes.py:101: UserWarning: Given trait value dtype \"int64\" does not match required type \"uint32\". A coerced copy has been created.\n",
      "  np.dtype(self.dtype).name))\n"
     ]
    }
   ],
   "source": [
    "import k3d\n",
    "import numpy as np\n",
    "\n",
    "points_number = 11200\n",
    "positions = 50 * np.random.random_sample((points_number,3)) - 25\n",
    "colors = np.random.randint(0, 0x777777, points_number)\n",
    "\n",
    "plot = k3d.plot()\n",
    "\n",
    "p = k3d.points(positions, colors, point_size=1)\n",
    "plot += p\n",
    "\n",
    "\n",
    "sigma=10.0\n",
    "beta=8./3\n",
    "rho=28.0\n",
    "def lorenz_deriv(X, sigma=sigma, beta=beta, rho=rho):\n",
    "    \"\"\"Compute the time-derivative of a Lorenz system.\"\"\"\n",
    "    x, y, z = X.T\n",
    "    return np.vstack([sigma * (y - x), x * (rho - z) - y, x * y - beta * z]).T\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = p.positions\n",
    "colors = p.colors\n",
    "m = (X[:,0]-.5*X[:,1])>0\n",
    "colors = np.random.randint(0, 0x777777, points_number)\n",
    "\n",
    "colors[m]=1\n",
    "colors[~m]=0xff0000\n",
    "\n",
    "p.colors = colors\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ed77051e57a04180ad09c9506db4e471",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Output()"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.camera_auto_fit = False\n",
    "plot.grid_auto_fit = False\n",
    "\n",
    "X = p.positions\n",
    "for i in range(10000):\n",
    "    X = X + lorenz_deriv(X, sigma=sigma, beta=beta, rho=rho)*0.005\n",
    "    if i%1==0 and i>0:\n",
    "        p.positions = X[::1,:]\n",
    "p.positions = X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%time  X = X + lorenz_deriv(X, sigma=sigma, beta=beta, rho=rho)*0.005\n",
    "%time  p.positions = X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%time  p.positions = X[::10,:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "p.point_size =1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "nbTranslate": {
   "displayLangs": [
    "en",
    "pl"
   ],
   "hotkey": "alt-t",
   "langInMainMenu": true,
   "sourceLang": "pl",
   "targetLang": "en",
   "useGoogleTranslate": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
